<div class="wrapper wrapper-content">
    <div class="row">
        <div class="toast"></div>
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <lqf_link><a id="data_backup" class="btn btn-info" url="{:url('dataBackup')}"><i
                            class="fa fa-download"></i> {:lang('backup data')}</a></lqf_link>
                    <lqf_link><a id='optimize' class="btn btn-info" url="{:url('optimize')}"><i
                            class="fa fa-shield"></i> {:lang('optimization')}</a></lqf_link>
                    <lqf_link><a id='repair' class="btn btn-info" url="{:url('repair')}"><i class="fa fa-wrench"></i> {:lang('repair')}</a></lqf_link>

                </div>
                <div class="ibox-content">
                    <form id="export-form" method="post" action="{:url('dataBackup')}">
                        <table class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th>{:lang('table name')}</th>
                                <th>{:lang('engine')}</th>
                                <th>{:lang('annotation')}</th>
                                <th>{:lang('data volume')}</th>
                                <th>{:lang('data szie')}</th>
                                <th>{:lang('backup status')}</th>
                                <th>{:lang('backup data')}</th>
                            </tr>
                            </thead>
                            {notempty name='list'}
                            <tbody>
                            {volist name='list' id='vo'}
                            <tr>
                                <td>{$vo.name}</td>
                                <td>{$vo.engine}</td>
                                <td>{$vo.comment}</td>
                                <td>{$vo.rows}</td>
                                <td>{$vo.data_length|format_bytes}</td>
                                <td class="info">{:lang('未备份')}</td>
                                <td>{$vo.create_time}<input type="hidden" name="tables[]" value="{$vo.name}"></td>
                            </tr>
                            {/volist}
                            </tbody>
                            {else/}
                            <tbody>
                            <tr class="odd">
                                <td colspan="6" class="text-center" valign="top">{:config('empty_list_describe')}</td>
                            </tr>
                            </tbody>
                            {/notempty}
                        </table>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $("#optimize").click(function () {
        sendUrlRequest('optimize');
    });
    $("#repair").click(function () {
        sendUrlRequest('repair');
    });
    function sendUrlRequest(id) {
        $('.fakeloader').show();
        $.post($("#" + id).attr("url"), {}, function (data) {
                $('.fakeloader').hide();
                if (data.code == 1) {
                    toast.success(data.msg);
                } else {
                    toast.error(data.msg);
                }
            }, "json"
        );
    }
    var $form = $("#export-form"), tables;
    $("#data_backup").click(function () {
        layer.msg('{:lang('备份中')}...{:lang('请勿关闭浏览器')}');
        $.post("{:url('dataBackup')}", $form.serialize(),
            function (data) {
                if (data.status) {
                    tables = data.tables;
                    backup(data.tab);
                } else {
                    toast.error(data.msg);
                }
            },
            "json"
        );
        return false;

    });

    function backup(tab, status) {
        status && showmsg(tab.id, "{:lang('开始备份')}...(0%)");
        $.get($form.attr("action"), tab, function (data) {
            if (data.status) {
                showmsg(tab.id, data.msg);
                if (!$.isPlainObject(data.tab)) {
                    window.onbeforeunload = function () {
                        return "{:lang('备份中')}...{:lang('请勿关闭浏览器')}！"
                    }
                    return;
                }
                backup(data.tab, tab.id != data.tab.id);
            } else {
                toast.error(data.msg);
            }
        }, "json");

    }
    function showmsg(id, msg) {
        layer.msg(msg);
        $('#export-form').find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
    }
</script>